package com.schedule.task.schedule;

import com.schedule.task.entity.BookUser;
import com.schedule.task.service.BookUserService;
import com.schedule.task.util.logfree.FreeWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 用户级别是否到期检测
 * 到期设置用户rank = 0
 */
@Component
public class RankCheckingTask {


    private final BookUserService bookUserService;

    @Autowired
    public RankCheckingTask(BookUserService bookUserService) {
        this.bookUserService = bookUserService;
    }

    @Scheduled(cron = "0 30 0 * * ?")
    public void startTaskTest() {
        //查找全部用户
        List<BookUser> bookUsers = bookUserService.allUser();
        //for循环判断是否过期
        if (!bookUsers.isEmpty()) {
            bookUsers.parallelStream().forEach(bookUser -> new MulTasks(bookUser, bookUserService).start());
//            for (BookUser bookUser: bookUsers) {
//                this.dealUser(bookUser);
//                MulTasks mulTasks = new MulTasks(bookUser, bookUserService);
//                mulTasks.start();
//            }
        }
        //过期就将用户设置为rank = 0 limit = -1
    }


    private class MulTasks extends Thread {

        private BookUser bookUser;
        private BookUserService bookUserService;

        public MulTasks(BookUser bookUser, BookUserService bookUserService) {
            this.bookUser = bookUser;
            this.bookUserService = bookUserService;
        }


        @Override
        public void run() {
            super.run();
            this.dealUser(bookUser);
        }

        private synchronized void dealUser(BookUser bookUser) {

            //获取时间
            Date date = new Date();
            String s = "yyyy-MM-dd";
            LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            String format = localDateTime.format(DateTimeFormatter.ofPattern(s));
            File fileDir = new File("../rank/" + format);
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            //判断用户是否过期
            if (bookUser.getRank() > 0 && bookUser.getRankAt() > 0) {

                //拿取rank_at 跟 limited 跟当前时间对比
                //当前时间
                Integer currentTimestamp = (int) (System.currentTimeMillis() / 1000);
                //用户过期时间戳
                Integer overTimestamp = bookUser.getRankAt() + (bookUser.getLimited() * 24 * 60 * 60);
                if (currentTimestamp > overTimestamp) {
                    String fileName = fileDir + "/" + format + ".deal.log";
                    try {
                        FreeWriter.logFree(fileName, "用户id:" + bookUser.getId() + " rank:" + bookUser.getRank() + " 过期已处理");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    this.bookUserService.changeRank(0, bookUser.getId());

                } else {
                    String fileName = fileDir + "/" + format + ".no_over.log";
                    try {
                        FreeWriter.logFree(fileName, "用户id:" + bookUser.getId() + " rank:" + bookUser.getRank() + " 不用处理");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            } else {

                String fileName = fileDir + "/" + format + ".no.log";
                try {
                    FreeWriter.logFree(fileName, "用户id:" + bookUser.getId() + " rank:" + bookUser.getRank() + " 不用处理");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
